<template>
  <el-dialog :title="info.dialogTitle" :visible.sync="dialogVisible" width="30%">
  <el-input v-model="input" placeholder="请输入内容" :class="{ 'visible': action}"></el-input>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitHandle">确 定</el-button>
  </span>
</el-dialog>
</template>

<script>
 export default {
    props:['info'],
    data() {
      return {
        dialogVisible: false,
        action:false,
        input:''
      };
    },
    methods: {
      async insertItemCategory(params){
        let res = await this.$api.insertItemCategory(params);
        console.log(res);
        if(res.data.status===200){
          this.$message({
                message: '添加成功',
                type: 'success'
          });
        }
      },
      async updateCategory(params){
        let res = await this.$api.updateCategory(params);
        console.log(res);
        if(res.data.status===200){
          this.$message({
                message: '更新成功',
                type: 'success'
          });
        }
      },
      async insertCategory(name){
        let res = await this.$api.insertCategory({name});
        console.log(res);
        if(res.data.status===200){
          this.$message({
                message: '新增成功',
                type: 'success'
          });
        }
      },
      submitHandle(){
        if(this.info.type===1){
          console.log('新增操作');
          let cid=this.info.id;
          let name=this.input;
          this.insertItemCategory({cid,name});
        }else if(this.info.type===2){
          console.log('更新操作');
          let id=this.info.id;
          let name=this.input;
          this.updateCategory({id,name});
          
        }else{
          console.log('新增一级分类');
          let name=this.input;
          this.insertCategory(name)
        }
        this.$parent.getitemCategory();
        this.dialogVisible = false;
      }
    }
  };
</script>

<style lang="less" scoped>
.visible{
    display: none;
}
</style>